<?php

namespace App\Console\Commands;

use App\Jobs\LotteryApplyJob;
use App\Models\Common\Common;
use App\Models\Common\Wechat;
use App\Models\Tools\Data;
use App\Models\Tools\Period;
use Carbon\Carbon;
use EasyWeChat\Kernel\Messages\Text;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class LotteryMessage extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:lottery_message';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '检查当前城市正在进行的霸王餐，如果存在,推送一次全员通知';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        // 查询所有存在正在进行活动的城市
        $activity_cities = DB::table('lottery_activity as la')
            ->leftJoin('shop','la.shop_id','=','shop.shop_id')
            ->where('la.status','=',Data::DATA_DB_LOTTERY_ACTIVITY_STATUS_BEFORE)
            ->distinct('city')
            ->pluck('city')
            ->toArray();

        $openids = DB::table('talent')
            ->leftJoin('lottery_users as lu','talent.we_id','=','lu.we_id')
            ->whereIn('talent.city',$activity_cities)
            ->where('talent.status','=',1)
            ->pluck('lu.openid')
            ->toArray();

        if(!count($openids)){
            return;
        }
        // 发送微信推送给所在地区所有达人
        $app = Wechat::officialAccount();
        $message = '有新的霸王餐发布，快来参与噢~<a href="'.env('APP_LAIFEI_URL').'/lottery">👉点此参与</a>';
        $message = new Text($message);
        // 发送霸王餐活动信息
        $err = [];
        $success = [];
        foreach($openids as $openid){
            // 发送微信推送给所在地区所有达人
            try{
                $result = $app->customer_service->message($message)->to($openid)->send();
                if($result['errcode'] !== 0){
                    if(isset($err[$result['errcode']])){
                        $err[$result['errcode']][] = $openid;
                    } else {
                        $err[$result['errcode']] = [$openid];
                    }
                } else {
                    $success[] = $openid;
                }
            }catch (\Exception $e){
                Log::emergency(LotteryMessage::class,['openid'=>$openid,'msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
            }
        }
        Log::debug('霸王餐活动推送',['openids'=>$openids,'success'=>$success,'err'=>$err]);
//        try{
//            $data = [
//                'first' => '有新的霸王餐活动发布啦！',
//                'keyword1' => '霸王餐',
//                'keyword2' => '正在进行',
//                'keyword3' => '点击【查看详情】立即参与 👇',
//                'remark' => '⭕ 中奖者到店免费体验',
//            ];
//            //发送通知
//            Common::sendLaifeiMessages($openids,'sign_up',$data,'/lottery');
//        }catch (\Exception $e){
//            Log::emergency(LotteryMessage::class,['openids'=>$openids,'msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
//        }

        return;

    }
}
